package com.ship.dispatch.service.impl;

import com.ship.common.core.exception.BusException;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.fob.SpFobTemplateFlie;
import com.ship.dispatch.mapper.SpFobTemplateFlieMapper;
import com.ship.dispatch.service.SpFobTemplateFlieServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
//import org.springframework.web.reactive.function.client.WebClient;

import java.net.URL;
import java.net.URLDecoder;
import java.util.Date;

@Service
public class SpFobTemplateFlieServerImpl extends BaseServiceImpl<SpFobTemplateFlieMapper, SpFobTemplateFlie> implements SpFobTemplateFlieServer {

    @Autowired
    SpFobTemplateFlieMapper spFobTemplateFlieMapper;
    @Value("${file_server.host}")
    private String fileServerHost;
    private static final String FILE_UPLOAD_PATH = "/group1/upload";
//    private final WebClient webClient;

//    public SpFobTemplateFlieServerImpl(WebClient webClient) {
//        this.webClient = webClient;
//    }

    @Override
    public SpFobTemplateFlie upload(FilePart file) {
        SpFobTemplateFlie result;
        try {
            // 构建 MultipartBodyBuilder
            MultipartBodyBuilder builder = new MultipartBodyBuilder();
            builder.part("file", file);

            String resp = "";
            System.out.println(resp);
            URL url = new URL(resp);
            String URL = resp.split("\\?")[0]; // 使用问号作为分隔符进行分割，取第一个部分
            String fileNameWithQuery = url.getFile();
            String fileNameWithoutQuery = fileNameWithQuery.substring(fileNameWithQuery.lastIndexOf("/") + 1);
            String fileName = URLDecoder.decode(fileNameWithoutQuery, "UTF-8");
            int queryIndex = fileName.indexOf('?');
            if (queryIndex != -1) {
                fileName = fileName.substring(0, queryIndex);
            }
            int dotIndex = fileName.lastIndexOf('.');
            if (dotIndex != -1) {
                fileName = fileName.substring(0, dotIndex);
            }
            SpFobTemplateFlie spFobTemplateFlie = new SpFobTemplateFlie();
            spFobTemplateFlie.setUrl(URL);
            spFobTemplateFlie.setFileName(fileName);
            spFobTemplateFlie.setCreateBy(SecurityUtils.getUsername());
            spFobTemplateFlie.setCreateDate(new Date());
            super.save(spFobTemplateFlie);
            Long id = spFobTemplateFlie.getId();
            System.out.println(id);
            result = spFobTemplateFlie;
        } catch (Exception e) {
            throw new BusException("文件服务器连接失败",BaseResultCode.GENERAL_ERROR);
        }
        return result;
    }

    @Override
    public void delByTemplateId(Long id) {
        spFobTemplateFlieMapper.delByTemplateId(id);
    }
}
